<?php
namespace Admin\Controller;
use think\controller;
class TagController extends AuthController{
    //所有的标签列表
    public function index(){
        $this->page_title='خەتكۈچلەر';
        $this->tags=M('tag')->order('tsort asc')->select();
        $this->display();
    }
//添加列表
    public function add(){
        $this->page_title='خەتكۈچ قوشۇش';
        $this->display();
    }

    //提交标签表单
    public function addhend()
    {
        $tname=$_POST['tname'];
        $post=I('post.');
        if(empty($tname)){
            $this->error('标签名称不能为空！');
        }
        //查找标签名是否存在
        $is=M('tag')->select();
        for ($i=0;$i<30;$i++){
            if($tname==$is[$i]['tname']){
                $this->error('标签名不能重复！');
            }
        }

        $tnames=explode('|',$_POST['tname']);
        foreach ($tnames as $tname){
            $tname=trim($tname);
            if($tname!=''){
                $tname=htmlspecialchars($tname);
                $tags=[
                    'tname'=>$tname,
                    'tsort'=>$_POST['tsort'],
                ];
            }
            M('tag')->add($tags);
        }
        $this->success('添加成功了',U('Tag/index'),1);
    }

    //删除tag
    public function deleteSort()
    {
        $tid=I('tid');
        if(M('tag')->where(array('tid'=>$tid))->delete())
        {
            $data=['status'=>1,'massage'=>'ئۆچۈرۈش تامام'];
        }
        else{
            $data=['status'=>0,'massage'=>'مەغلۇب بولدى'];
        }
        $this->ajaxReturn($data);
    }


    //编辑
    public function edit($tid)
    {
        $this->page_title='خەتكۈچ تەھرىرلەش';
        $this->tags=M('tag')->where(array('tid'=>$tid))->find();
        $this->display();
    }
    //提交标签表单
    public function savehend()
    {
        if(!M('tag')->save(I('post.'))) $this->error('ساقلاش مەغلۇب بولدى، قايتا سىناڭ!');
        $this->success('ساقلاش تامام',U('Tag/index'),1);
    }
}